package com.drugStorage.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.drugStorage.domain.BuyList;
import com.drugStorage.domain.BuyPlan;
import com.drugStorage.hibernate.Utils.HibernateUtils;
import com.drugStorage.hibernate.dao.BuyPlanDao;

public class BuyPlanDaoImpl implements BuyPlanDao {

	private static final String LOAD_ALL = "from BuyPlan";
	private static final String LOAD_ISCHECKED = "from BuyPlan where ischecked = ?";
	private static final String LOAD_BUYLIST_BY_BUYPLANID = "from BuyList b where b.buyPlan.id=?";
	
	public void DelBuyPlan(BuyPlan buyPlan) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(buyPlan);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public void SaveBuyPlan(BuyPlan buyPlan) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.merge(buyPlan);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}
	}

	public void UpdateBuyPlan(BuyPlan buyPlan) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.saveOrUpdate(buyPlan);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public BuyPlan getBuyPlanById(int id) {
		Session s = null;

		try {
			s = HibernateUtils.getSession();

			Criteria c = s.createCriteria(BuyPlan.class);
			c.add(Restrictions.eq("id", id));
			BuyPlan buyPlan = (BuyPlan) c.uniqueResult();
			return buyPlan;

		} finally {
			if (s != null)
				s.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<BuyPlan> getBuyPlanListByIschecked(int isChecked) {

		Session s = null;
		try {
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_ISCHECKED);
           q.setParameter(0, isChecked);
			return q.list();

		} finally {
			if (s != null)
				s.close();

		}
	}

	public void AddBuyListByPlanId(BuyList buyList) {
	Session s = null;
	Transaction tx = null;
	try{
		s = HibernateUtils.getSession();
		tx = s.beginTransaction();
		s.save(buyList);
		tx.commit();
	}finally{
		if(s!=null)
			s.close();
	}

	}

	@SuppressWarnings("unchecked")
	public List<BuyList> getBuyListByPlanId(int Planid) {
		
		Session s = null;
		try {
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_BUYLIST_BY_BUYPLANID);
              q.setParameter(0, Planid);
              List<BuyList> l = q.list();
//              for(BuyList b:l){
//              Hibernate.initialize(b.getDrug().getName());
//              }
			return l;

		} finally {
			if (s != null)
				s.close();

		}
	}

	@SuppressWarnings("unchecked")
	public List<BuyPlan> getAllBuyPlan() {
		Session s = null;
		try{
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_ALL);
			return q.list();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	public void DelBuyList(BuyList buyList) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(buyList);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}
	}

}
